﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;

namespace Mindfor.Web.Data.Mappings
{
	public class PageMap : ClassMap<Page>
	{
		public PageMap()
		{
			Table("Pages");
			Id(m => m.Id);
			Map(m => m.Name).Length(255).Not.Nullable();
			Map(m => m.Sort);
			Map(m => m.CreateDate);
			Map(m => m.ShowInMenu);
			Map(m => m.AllowAnonymous);
			Map(m => m.AllowAuthorized);
			Map(m => m.OpenInNewWindow);
			Map(m => m.Image).CustomSqlType("image").Length(int.MaxValue);
			Component(m => m.ActionPath, a =>
			{
				a.Map(x => x.ModuleName).Length(255).Nullable();
				a.Map(x => x.ControllerName).Length(255).Nullable();
				a.Map(x => x.ActionName).Length(255).Nullable();
			});
			References(m => m.Parent, "ParentId");

			HasMany<Page>(m => m.ChildPages).KeyColumn("ParentId");
			HasMany<PageText>(m => m.Texts).KeyColumn("PageId").Inverse().Cascade.AllDeleteOrphan();
			HasManyToMany<Role>(m => m.AllowRoles).Table("PageRoles").ParentKeyColumn("PageId").ChildKeyColumn("RoleId");
			HasManyToMany<Tag>(m => m.Tags).Table("PageTags").ParentKeyColumn("PageId").ChildKeyColumn("TagId");
		}
	}
}
